<?php
date_default_timezone_set('Asia/Saigon');
?>
<?php
# MantisBT - a php based bugtracking system

# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.

	/**
	 * @package MantisBT
	 * @copyright Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
	 * @copyright Copyright (C) 2002 - 2010  MantisBT Team - mantisbt-dev@lists.sourceforge.net
	 * @link http://www.mantisbt.org
	 */
	 /**
	  * MantisBT Core API's
	  */
	require_once( 'core.php' );
	require_once( 'current_user_api.php' );
	require_once( 'compress_api.php' );
	require_once( 'filter_api.php' );
	require_once( 'last_visited_api.php' );
	//require_once( 'communication_api.php' );
	
	//requestmysql(mantis_care_report_table);
	auth_ensure_user_authenticated();

	$t_current_user_name = current_user_get_field( 'username' );
	$t_current_real_name = current_user_get_field( 'realname' );
	
	# Improve performance by caching category data in one pass
	category_get_all_rows( helper_get_current_project() );

	compress_enable();

	# don't index my view page
	html_robots_noindex();

	html_page_top1( lang_get( 'my_view_link' ) );

	if ( current_user_get_pref( 'refresh_delay' ) > 0 ) {
		html_meta_redirect( 'my_view_page.php', current_user_get_pref( 'refresh_delay' )*60 );
	}

	html_page_top2();

	print_recently_visited();

	$f_page_number		= gpc_get_int( 'page_number', 1 );

	$t_per_page = config_get( 'my_view_bug_count' );
	$t_bug_count = null;
	$t_page_count = null;

	$t_boxes = config_get( 'my_view_boxes' );
	asort ($t_boxes);
	reset ($t_boxes);
	#print_r ($t_boxes);

	$t_project_id = helper_get_current_project();
?>
<br>
Trung tâm quản lí dữ liệu cuộc gọi<br>
================================================================================================
<table border=0 cellspacing="3" cellpadding="0" width=100%>
			<tr>
				<td align="center">[<a href="?do=addcare" title="Nhập thông tin khách hàng">X Call</a>] 
				| [<a href="?do=viewall" title="Liệt kê danh sách khách hàng">X ViewAll</a>] 
				| [<a href="?do=viewme">X ViewMe</a>] 
				| [<a href="?do=viewinc" title="Liệt kê khách hàng gọi đến">X ViewIn]</a>
				| [<a href="?do=viewout" title="Liệt kê khách hàng gọi đi">X ViewOut</a>] 
				| [<a href="?do=creport">X report</a>] 
				| [<a href="coogle.php">X Search</a>] 
				</td>
			</tr>
</table>

<div align="center">
<table border=0 cellspacing="3" cellpadding="0" width=100%>
			<tr>
				<td>
				<?php 
				$do = $_GET['do'];
				echo '<form action="?do=upload&id='.$id.'" method="post" enctype="multipart/form-data">
								<table border=1 cellpadding=0 cellspacing=0 style="border-collapse:collapse" width=100%><tr><td>
								<b>Upload File [V]<br><br>
								<table border=0 cellpadding=0 cellspacing=0 style="border-collapse:collapse" width=100% bgcolor=#CECEFF>
								<tr><td width=20%>Select File<br>(Maximum size: 5,000k)</td></td><td> <input type="file"  name="ufile" /> <input type="submit" value="Upload file" /></td></tr>
								</table></td></tr></table>
								</form><br>';
					#Upload file log, screenshot theo bai viet
		if ($do == upload)
	{
		$id = $_GET['id'];
		$SafeFile = $HTTP_POST_FILES['ufile']['name'];
		$SafeFile = str_replace("#", "No.", $SafeFile);
		$SafeFile = str_replace("$", "Dollar", $SafeFile);
		$SafeFile = str_replace("%", "Percent", $SafeFile);
		$SafeFile = str_replace("^", "", $SafeFile);
		$SafeFile = str_replace("&", "and", $SafeFile);
		$SafeFile = str_replace("*", "", $SafeFile);
		$SafeFile = str_replace("?", "", $SafeFile);

		$t_current_user_name = current_user_get_field( 'username' );
						// random 4 digit to add to our file name
						// some people use date and time in stead of random digit
						$random_digit=rand(00000000000000,99999999999999);
						//combine random digit to you file name to create new file name
						$new_file_name=$random_digit.$theFileName;
		$path= "upload/".$new_file_name;
		
		if($ufile != none)
		{ //AS LONG AS A FILE WAS SELECTED...

		if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))//IF IT HAS BEEN COPIED...
		{ 
			//GET FILE NAME
			$theFileName = $HTTP_POST_FILES['ufile']['name'];

			//GET FILE SIZE
			$theFileSize = $HTTP_POST_FILES['ufile']['size'];
			
			//GET FILE SIZE
			$theFileType = $HTTP_POST_FILES['ufile']['type'];
			

			if ($theFileSize>999999){ //IF GREATER THAN 999KB, DISPLAY AS MB
				$theDiv = $theFileSize / 1000000;
				$theFileSize = round($theDiv, 1)." MB"; //round($WhatToRound, $DecimalPlaces)
			} else { //OTHERWISE DISPLAY AS KB
				$theDiv = $theFileSize / 1000;
				$theFileSize = round($theDiv, 1)." KB"; //round($WhatToRound, $DecimalPlaces)
			}
			$mark = 'down';
			$care_id = $id;
			
			$user_id = $t_current_user_name;
			$date_added = time();
			$md5 = md5_file($path);
			echo $md5;
			mysql_query("insert into `mantis_care_file_table` 
				(filename,file_type,filesize,date_added,care_id, pjcare_id, user_id, folder, new_file_name) 
				values ('$theFileName','$theFileType','$theFileSize','$date_added','$care_id','$pjcare_id','$user_id','$path','$new_file_name')");
		} else 
			{


		//PRINT AN ERROR IF THE FILE COULD NOT BE COPIED
				echo '
				<table cellpadding="5" width="80%">
				<tr>
				<td align="Center" colspan="2"><font color=\"#C80000\"><b>File could not be uploaded</b></font></td>
				</tr></table>';
			}
		}
		header("location: /mantis/xcenter.php");
	}
				#Show file upload
				echo '
				<table border=1 cellpadding=0 cellspacing=0 style="border-collapse:collapse" width=100% class=ttt><tr><td>
				<b>Attached Files [A]<br><br>
				<table border=0 cellpadding=0 cellspacing=0 style="border-collapse:collapse" width=100% bgcolor=#CECEFF>
				<tr><td width=20%><b>Filename</td><td><b>Date added</td><td><b>User_id</td><td><b>Download/View on site</td></tr>';
											
				$sqlfUp = mysql_query('SELECT  id,filename,file_type,filesize,from_unixtime(date_added), care_id, user_id,folder, new_file_name FROM mantis_care_file_table WHERE care_id="'.$id.'"');
				$b = 1;
				While ($row = mysql_fetch_array($sqlfUp))
				{	
					if($b%2==0)  
					{ $sl = '#E1E1E1';}else{ $sl = 'white';}
					$filename    = $row['filename'];
					$file_type   = $row['file_type'];             
					$filesize = $row['filesize'];
					$date_added = $row['from_unixtime(date_added)'];
					$care_id = $row['care_id'];
					$user_id       = $row['user_id'];
					$folder        = $row['folder'];
					$new_file_name= $row['new_file_name'];
					$url = ''.$folder;
					echo '<tr><td bgcolor='.$sl.'>'. $filename.'</td><td bgcolor='.$sl.'>'.$date_added.'</td><td bgcolor='.$sl.'>'.$user_id.'</td><td bgcolor='.$sl.'><a href='.$url.'>Download</a></td></tr>';
					$b++;
					}
					echo '</table></td></tr></table>
					</form><br>
				';

				//Release thong tin file xCenter
				$file = "D:\\AppServ\\www\\mantisBT\\mantis\\upload\\$new_file_name";
				$line = file($file);
				$cLine= shell_exec('type D:\\AppServ\\www\\mantisBT\\mantis\\upload\\'.$new_file_name.' | find /C "1"');
			for ($i=0;$i<$cLine;$i++)
			{
				echo "<b><u>line".$i."</u></b> ";
				
				/* #In chuỗi ngày tháng
				$pattern1 = '/^\d.\d{2}.\d{2}./i';
				echo $match1[0];  
				preg_match($pattern1, $line["$i"], $match1);*/
			#In tháng cuộc gọi
				$matchesMonth = array();
				preg_match('/^\d/', $line["$i"], $matchesMonth);
			#In ngày cuộc gọi
				$matchesDATE = array();
				preg_match('/(\d{2})/', $line["$i"], $matchesDATE);
			#In năm cuộc gọi
				$matchesYEAR = array();
				preg_match('/(\d{2}) /', $line["$i"], $matchesYEAR);
			
			echo 'DATE: '.$matchesMonth[0].'/'.$matchesDATE[0].'/'.$matchesYEAR[0];
				
			#In giờ cuộc gọi
				$matchesHOUR = array();
				preg_match('/ (.*?)([:])/s', $line["$i"], $matchesHOUR);
			#In phút cuộc gọi
				$matchesMINUTE = array();
				preg_match('/:(.*?)([A-Z])/s', $line["$i"], $matchesMINUTE);
			#Cuộc gọi AM/PM
				$matchesAPM = array();
				preg_match('/([AP]M)/s', $line["$i"], $matchesAPM);
			
			echo $matchesHOUR[1].':'.$matchesMINUTE[1].''.$matchesAPM[1];
				
				#In line nhận cuộc gọi
				$patternLINEIN = '/(\d{3})/';
				preg_match($patternLINEIN, $line["$i"], $match3);
				echo $match3[0];
				
				#In line trên tổng đài
				$patternLINEC = '/( \d{2} )/';
				preg_match($patternLINEC, $line["$i"], $match4);
				echo $match4[0];
				
				#In status cuộc gọi
				$patternCALLST = '/( incoming )/i';
				preg_match($patternCALLST, $line["$i"], $match5);
				echo $match5[0];
				 
				#In number phone incoming
				$patternPHONEN = '/((\d{9,12}) )/i';
				preg_match($patternPHONEN, $line["$i"], $match6);
				echo $match6[0]; 
				 
			/* 	# Match time incoming
				$pattern7 = "/(')/";
				$replace7 = '@';
				$r7 = preg_replace($pattern7, $replace7, $line["$i"]);
				$pattern8 = '/(")/';
				$replace8 = '.';
				$r8 = preg_replace($pattern8, $replace8, $r7);
				$pattern9 = '/ (\d{2})(:)(\d{2})(@)(\d{2})(.)/';
				preg_match($pattern9, $r8, $matchTimeR7);
				echo $matchTimeR7[0].' ';*/
			#In số giờ gọi
				$matchesLONGHOUR = array();
				preg_match('/ 0(0|1|2|3)/', $line["$i"], $matchesLONGHOUR);
			#In số phút gọi
				$matchesLONGMI = array();
				preg_match('/\:(\d{2})\'/s', $line["$i"], $matchesLONGMI);
			#In số giấy gọi
				$matchesSECOND = array();
				preg_match('/\'(.*?)\"/s', $line["$i"], $matchesSECOND);
			
			$callLong = $matchesLONGHOUR[0]*60*60 + $matchesLONGMI[1]*60 + $matchesSECOND[1];
			echo '<u><b>Tổng thời gian gọi là: '.$callLong.' giây</b></u> '.$matchesLONGHOUR[0].':'.$matchesLONGMI[1].':'.$matchesSECOND[1].'<br>';
				
			mysql_query("insert into `mantis_xcenter_table` 
			(agentName,agentRealName,agentNumber,agentCardNumber,callStatus, callDate, callMonth, callYear, callH, callM, callAPM, callLong, callGuest) 
				values ('$t_current_user_name','$t_current_real_name','$match3[0]','$match4[0]','$match5[0]','$matchesDATE[0]','$matchesMonth[0]','$matchesYEAR[0]','$matchesHOUR[1]','$matchesMINUTE[1]','$matchesAPM[1]','$callLong','$match6[0]')");
			}
				
	//Ghi dữ liệu khách hàng vào data
		if ($do == pdata)
		{
						echo '<html xmlns="http://www.w3.org/1999/xhtml">
						<head>
						<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
						<title>Seller management Systems</title>
						<link rel="stylesheet" href="css/style.css" type="text/css">
						</head>
						<body>
						';
						$seller = $t_current_user_name;
						//send information from form
						$spcode = $_POST['spcode'];
						$name = $_POST['name'];
						$phone = $_POST['phone'];
						$email = $_POST['email'];
						$sex = $_POST['sex'];
						$address = $_POST['address'];
						$city = $_POST['city'];
						$serial = $_POST['serial'];
						$lo = $_POST['lo'];
						$dt = $_POST['dt'];
						$object = $_POST['object'];
						$status = $_POST['status'];
						$state = $_POST['itemtype'];
						$confirm = $_POST['confirm'];
						$process = $_POST['process'];
						$receiver = $_POST['receiver'];
						$time = date("H:i:s, d-m-Y");
						$start_time = time();
						
						if (preg_match('/^\(?[0]{1}[0-9]{2}\)?|[0-9]{3,3}?[ ][0-9]{3,3}?[0-9]{4,4}$/', $phone)) 
						
						{ 	//Them so vao Serial/Lo
							if(strlen($serial) == 4) {	$serial = '0'.$serial;}
							if(strlen($serial) == 3) {	$serial = '00'.$serial;}
							if(strlen($serial) == 2) {	$serial = '000'.$serial;}
							if(strlen($serial) == 1) {	$serial = '0000'.$serial;}
							//Insert contact khách hàng
								mysql_query("
								insert into `$db`.`contact` 
								(name, phone, email, address) 											
								values('$name','$phone','$email','$address')");
								echo '<div align=center><table border=1><tr><td align=center>';
							if ($state == complete){
							//Insert thong tin cham soc khach hang
								$mark = complete;
								mysql_query("
								insert into `mantis_care_report_table` 
								(spcode, name, phone, email, sex, address, city, serial , lo, dt, object, status, state, confirm, process, mark ,start_time, carer) 											
								values('$spcode','$name','$phone','$email','$sex','$address','$city','$serial','$lo','$dt','$object','$status','$state','$confirm','$process', '$mark','$start_time','$seller')");
								echo "Bạn đã nhập dữ liệu khách hàng <b>".$name."</b> với số Serial/lo:[<b>".$serial."/".$lo."</b>] thành công<br>";
								echo "Check danh sách khách hàng chăm sóc trong ngày hôm nay";
								echo  "<div align=center><br><a href='?do=viewall'>[LIST]</a> | <a href='logout_page.php'>[Logout]</a></div>";
								}
							else 
							{							
							
							if ($state == move)
								{
								$mark = processing;
								
								mysql_query("
								insert into `mantis_care_report_table` 
								(spcode, name, phone, email, sex, address, city, serial , lo, dt, object, status, state, confirm, process, mark ,start_time, carer, receiver) 											
								values('$spcode','$name','$phone','$email','$sex','$address','$city','$serial','$lo','$dt','$object','$status','$state','$confirm','$process', '$mark','$start_time','$seller','$receiver')");
								echo "Bạn đã nhập dữ liệu khách hàng <b>".$name."</b> với số Serial/lo:[<b>".$serial."/".$lo."</b>] thành công<br>";
								echo "Check danh sách khách hàng chăm sóc trong ngày hôm nay";
								echo  "<div align=center><br><a href='?do=viewall'>[LIST]</a> | <a href='logout_page.php'>[Logout]</a></div>";
								}
								else {echo 'chua chon trang thai kia ku';}
							}
							echo '</td></tr></table></div>';	}
						else
							{ 
							echo "<div align=left>
							Bạn nhập sai số điện thoại rồi, nhập lại ngay<br>
							<b><u>Chú ý:</u></b><br>
							#Không có dấu cách ở đầu<br>#Không được thiếu đầu 0 ở đầu</div>";
							}
						
		}
		
		//Hiện thị toàn bộ danh khách hàng
		if ($do == viewall)
		{	
			$req = mysql_query("select * from `mantis_xcenter_table` ORDER BY  callID DESC");
			echo ' <html xmlns="http://www.w3.org/1999/xhtml">
					<head>
					<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
					<title>Seller management Systems</title>
					<link rel="stylesheet" href="css/default.css" type="text/css">
					<title></title> 
					</head>
					<body>	
					<table border=1 cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100%> 
						<tr>
							<td>  <b>Viewing all report</b> [ Print Reports ]  [ CSV Export ]  [ Excel Export ]<br><br>    
								<table border=1 bordercolor=white cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100% bgcolor=#CECEFF class=ttt>  
									<tr>
										<td width=4%>ID</td>
										<td width=4%>ED</td>
										<td width=10%>NAME</td>
										<td width=10%>CATEGORY</td>
										<td width=10%>STATUS</td>
										<td width=7%>Start time</td>
										<td width=7%>Last Update</td>
										<td width=7%>Start Supporter</td>
										<td width=7%>Last Supporter</td>
										<td width=22%>SUMMARY</td>
									</tr>
								</table><br>
								<table border=1 bordercolor=white cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100%>';
							$i = '0';
							
							while ($row = mysql_fetch_array($req))
							{	
									 $id = $row['callID'];
									 $agentName = $row['agentName'];
									 $agentRealName= $row['agentRealName'];
									 $agentNumber= $row['agentNumber'];
									 $agentCardNumber= $row['agentCardNumber'];
									 $callStatus= $row['callStatus'];
									 $callDate= $row['callDate'];
									 $callMonth= $row['callMonth'];
									 $callYear= $row['callYear'];
									 $callTime= $row['callTime'];
									 $callTimeL= $row['callTimeL'];
									 $callLong= $row['callLong'];
									 $callGuest= $row['callGuest'];
									
									$start_time = (date("d-m-Y H:i:s",$row['start_time']));
									$QueryDay = (date("d",$row['start_time']));
									$QueryCurrentDay = date("d");
									$QueryCurrentMonth = (date("m",$row['start_time']));
									$QueryMonth = date("m");
									
									if (($QueryDay == $QueryCurrentDay) and ($QueryCurrentMonth == $QueryMonth))
									{
									// Max a last update

												$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
												$resultt = mysql_query($qr) or die(mysql_error());
												
												$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
												$resultqt = mysql_query($qt) or die(mysql_error());
												// Print out result
													
									echo '<tr>
										<td width=4%   class='.$state.'>'.$i.'</td>
										<td width=4%   class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</a></td>
										<td width=10%  class='.$state.'>'.$name.'</td>
										<td width=10%  class='.$state.'>'.$object.'</td>
										<td width=10%  class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
										<td width=7%   class='.$state.'>'.$start_time.'</td>
										<td width=7%   class='.$state.'>';
													while($row = mysql_fetch_array($resultt))
															{
																$cid = $row['cid'];
																$note = $row['note'];
																$resolver = $row['resolver'];
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $last_updated;
															}	echo '</td>
										<td width=7%   class='.$state.'>'.$carer.'</td>
										<td width=7%   class='.$state.'>';
													while($row = mysql_fetch_array($resultqt))
															{
																$cid = $row['cid'];
																$note = $row['note'];
																$resolver = $row['resolver'];
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $resolver;
															}	echo'</td>
									
										<td width=22%  class='.$state.'c>'.substr($confirm,0,100).'</td>
										</tr>';					
									}
							}
			echo '			</table><br>			
							</td>
						</tr>
					</table>
					</body>
					</html>';
		}
		
		//View cac khach hang cua Current user
		
			//Hiện thị toàn bộ danh khách hàng
		if ($do == viewme)
		{
			$t_current_user_name = current_user_get_field( 'username' );
			
			$req = mysql_query("select * from `mantis_care_report_table` where carer='$t_current_user_name' ORDER BY id DESC");
			echo ' <html xmlns="http://www.w3.org/1999/xhtml">
					<head>
					<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
					<title>Seller management Systems</title>
					<link rel="stylesheet" href="css/default.css" type="text/css">
					<title></title> 
					</head>
					<body>	
					<table border=1 cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100%> 
						<tr>
							<td>  <b>Viewing all report</b> [ Print Reports ]  [ CSV Export ]  [ Excel Export ]<br><br>    
								<table border=1 bordercolor=white cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100% bgcolor=#CECEFF class=ttt>  
									<tr>
										<td width=4%>ID</td>
										<td width=4%>ED</td>
										<td width=10%>NAME</td>
										<td width=10%>CATEGORY</td>
										<td width=10%>STATUS</td>
										<td width=7%>Start time</td>
										<td width=7%>Last Update</td>
										<td width=7%>Start Supporter</td>
										<td width=7%>Last Supporter</td>
										<td width=22%>SUMMARY</td>
									</tr>
								</table><br>
								<table border=1 bordercolor=white cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100%>';
							$i = '0';
							while ($row = mysql_fetch_array($req))
							{	
									$i++;
									$id	= $row['id'];
									$name = $row['name'];
									$phone = $row['phone'];
									$email = $row['email'];
									$sex = $row['sex'];
									$address = $row['address'];
									$city = $row['city'];
									$serial = $row['serial'];
									$lo = $row['lo'];
									$object = $row['object'];
									$status = $row['status'];
									$state = $row['state'];
									$confirm = $row['confirm'];
									$process = $row['process'];
									$receiver = $row['receiver'];
									$carer = $row['carer'];
									$start_time = (date("d-m-Y H:i:s",$row['start_time']));
									// Max a last update

												$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
												$resultt = mysql_query($qr) or die(mysql_error());
												
												$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
												$resultqt = mysql_query($qt) or die(mysql_error());
												// Print out result
													
									echo '<tr>
										<td width=4%   class='.$state.'>'.$i.'</td>
										<td width=4%   class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</a></td>
										<td width=10%  class='.$state.'>'.$name.'</td>
										<td width=10%  class='.$state.'>'.$object.'</td>
										<td width=10%  class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
										<td width=7%   class='.$state.'>'.$start_time.'</td>
										<td width=7%   class='.$state.'>';
													while($row = mysql_fetch_array($resultt))
															{
																$cid = $row['cid'];
																$note = $row['note'];
																$resolver = $row['resolver'];
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $last_updated;
															}	echo '</td>
										<td width=7%   class='.$state.'>'.$carer.'</td>
										<td width=7%   class='.$state.'>';
													while($row = mysql_fetch_array($resultqt))
															{
																$cid = $row['cid'];
																$note = $row['note'];
																$resolver = $row['resolver'];
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $resolver;
															}	echo'</td>
									
										<td width=22%  class='.$state.'c>'.substr($confirm,0,100).'</td>
										</tr>';					
							}
			echo '			</table><br>			
							</td>
						</tr>
					</table>
					</body>
					</html>';
		}
		//View các khách hàng gọi đến
		if ($do == viewinc)
		{
			$req = mysql_query("select * from `mantis_care_report_table` where object='incomming' ORDER BY id DESC");
			echo ' <html xmlns="http://www.w3.org/1999/xhtml">
					<head>
					<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
					<title>Seller management Systems</title>
					<link rel="stylesheet" href="css/default.css" type="text/css">
					<title></title> 
					</head>
					<body>	
					<table border=1 cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100%> 
						<tr>
							<td>  <b>Viewing all report</b> [ Print Reports ]  [ CSV Export ]  [ Excel Export ]<br><br>    
								<table border=1 bordercolor=white cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100% bgcolor=#CECEFF class=ttt>  
									<tr>
										<td width=4%>ID</td>
										<td width=4%>ED</td>
										<td width=10%>NAME</td>
										<td width=10%>CATEGORY</td>
										<td width=10%>STATUS</td>
										<td width=7%>Start time</td>
										<td width=7%>Last Update</td>
										<td width=7%>Start Supporter</td>
										<td width=7%>Last Supporter</td>
										<td width=22%>SUMMARY</td>
									</tr>
								</table><br>
								<table border=1 bordercolor=white cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100%>';
							$i = 0;
							while ($row = mysql_fetch_array($req))
							{
									$i++;
									$id	= $row['id'];
									$name = $row['name'];
									$phone = $row['phone'];
									$email = $row['email'];
									$sex = $row['sex'];
									$address = $row['address'];
									$city = $row['city'];
									$serial = $row['serial'];
									$lo = $row['lo'];
									$object = $row['object'];
									$status = $row['status'];
									$state = $row['state'];
									$confirm = $row['confirm'];
									$process = $row['process'];
									$receiver = $row['receiver'];
									$carer = $row['carer'];
									$start_time = (date("d-m-Y H:i:s",$row['start_time']));
									$QueryDay = (date("d",$row['start_time']));
									$QueryCurrentDay = date("d");
									$QueryCurrentMonth = (date("m",$row['start_time']));
									$QueryMonth = date("m");
									
									if (($QueryDay == $QueryCurrentDay) and ($QueryCurrentMonth == $QueryMonth))
									{
									// Max a last update

												$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
												$resultt = mysql_query($qr) or die(mysql_error());
												
												$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
												$resultqt = mysql_query($qt) or die(mysql_error());
												// Print out result
													
									echo '<tr>
										<td width=4% class='.$state.'>'.$i.'</td>
										<td width=4% class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</td>
										<td width=10% class='.$state.'>'.$name.'</td>
										<td width=10% class='.$state.'>'.$object.'</td>
										<td width=10% class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
										<td width=7%  class='.$state.'>'.$start_time.'</td>
										<td width=7%  class='.$state.'>';
													while($row = mysql_fetch_array($resultt))
															{
																$cid = $row['cid'];
																$note = $row['note'];
																$resolver = $row['resolver'];
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $last_updated;
															}	echo '</td>
										<td width=7%  class='.$state.'>'.$carer.'</td>
										<td width=7%  class='.$state.'>';
													while($row = mysql_fetch_array($resultqt))
															{
																$cid = $row['cid'];
																$note = $row['note'];
																$resolver = $row['resolver'];
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $resolver;
															}	echo'</td>
									
										<td width=22%  class='.$state.'c>'.substr($confirm,0,100).'</td>
										</tr>';	
									}										
							}
			echo '			</table><br>			
							</td>
						</tr>
					</table>
					</body>
					</html>';
		}
		
		if ($do == viewout)
		{
			$req = mysql_query("select * from `mantis_care_report_table` where object='outgoing' ORDER BY id DESC");
			echo ' <html xmlns="http://www.w3.org/1999/xhtml">
					<head>
					<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
					<title>Seller management Systems</title>
					<link rel="stylesheet" href="css/default.css" type="text/css">
					<title></title> 
					</head>
					<body>	
					<table border=1 cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100%> 
						<tr>
							<td>  <b>Viewing all report</b> [ Print Reports ]  [ CSV Export ]  [ Excel Export ]<br><br>    
								<table border=1 bordercolor=white cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100% bgcolor=#CECEFF class=ttt>  
									<tr>
										<td width=4%>ID</td>
										<td width=4%>ED</td>
										<td width=10%>NAME</td>
										<td width=10%>CATEGORY</td>
										<td width=10%>STATUS</td>
										<td width=7%>Start time</td>
										<td width=7%>Last Update</td>
										<td width=7%>Start Supporter</td>
										<td width=7%>Last Supporter</td>
										<td width=22%>SUMMARY</td>
									</tr>
								</table><br>
								<table border=1 bordercolor=white cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100%>';
							$i = 0;
							while ($row = mysql_fetch_array($req))
							{
									$i++;
									$id	= $row['id'];
									$name = $row['name'];
									$phone = $row['phone'];
									$email = $row['email'];
									$sex = $row['sex'];
									$address = $row['address'];
									$city = $row['city'];
									$serial = $row['serial'];
									$lo = $row['lo'];
									$object = $row['object'];
									$status = $row['status'];
									$state = $row['state'];
									$confirm = $row['confirm'];
									$process = $row['process'];
									$receiver = $row['receiver'];
									$carer = $row['carer'];
									$start_time = (date("d-m-Y H:i:s",$row['start_time']));
									$QueryDay = (date("d",$row['start_time']));
									$QueryCurrentDay = date("d");
									$QueryCurrentMonth = (date("m",$row['start_time']));
									$QueryMonth = date("m");
									
									if (($QueryDay == $QueryCurrentDay) and ($QueryCurrentMonth == $QueryMonth))
									{
									// Max a last update

												$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
												$resultt = mysql_query($qr) or die(mysql_error());
												
												$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
												$resultqt = mysql_query($qt) or die(mysql_error());
												// Print out result
													
									echo '<tr>
										<td width=4%   class='.$state.'>'.$i.'</td>
										<td width=4%   class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</a></td>
										<td width=10%  class='.$state.'>'.$name.'</td>
										<td width=10%  class='.$state.'>'.$object.'</td>
										<td width=10%  class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
										<td width=7%   class='.$state.'>'.$start_time.'</td>
										<td width=7%   class='.$state.'>';
													while($row = mysql_fetch_array($resultt))
															{
																$cid = $row['cid'];
																$note = $row['note'];
																$resolver = $row['resolver'];
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $last_updated;
															}	echo '</td>
										<td width=7%  class='.$state.'>'.$carer.'</td>
										<td width=7%  class='.$state.'>';
													while($row = mysql_fetch_array($resultqt))
															{
																$cid = $row['cid'];
																$note = $row['note'];
																$resolver = $row['resolver'];
																$last_updated = (date("d-m-Y H:i:s",$row['MAX(last_updated)']));
																echo $resolver;
															}	echo'</td>
									
										<td width=22%  class='.$state.'c>'.substr($confirm,0,100).'</td>
										</tr>';	
									}
							}
			echo '			</table><br>			
							</td>
						</tr>
					</table>
					</body>
					</html>';
		}
		
		if ($do == creport)
		{	
			$er = current_user_get_field( 'access_level' );
			echo '<table border=1>
			<tr>
				<td></td>
				<td><u>Xuất báo cáo theo thời gian chỉ định</u><br><br></td>
			</tr>
			<form name=xreport method=POST action="?do=xreport&value=x">
			<tr>
				<td>Day: </td><td> From <input name=sDay type=sDay> to <input name=eDay type=eDay><br></td>
			</tr>
			<tr>
				<td>Month:</td><td> From <input name=sMonth type=sMonth> to <input name=eMonth type=eMonth>  <br></td>
			</tr>
			<tr>
				<td>Year: </td><td> From <input name=sYear type=sYear> to <input name=eYear type=eYear><br></td>
			</tr>';
			if ($er == 90)
			{
				echo 
				'<tr>
					<td>Agent</td><td><input name=cNameAgent type=cNameAgent></td>
				</tr>';
			}
			echo 
			'<tr>
				<td></td><td><input name=submit type=submit value=submit></td>
			</tr>
			</form>			
			</table>
			';
		}
		
		if (($do == xreport) and ($_GET['value'] == x))
		{
			
			$sDay = $_POST['sDay'];
			$sMonth = $_POST['sMonth'];
			$sYear = $_POST['sYear'];
			$eDay = $_POST['eDay'];
			$eMonth = $_POST['eMonth'];
			$eYear = $_POST['eYear'];
			$username = $_POST['cNameAgent'];
			echo '<br>
				<input type="button" onClick="window.print()" value="In báo cáo"/>
				<br><br>
				<table border=1 cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100% > 
						<tr>
							<td>  <b>Viewing all report</b> [ Print Reports ]  [ CSV Export ]  [ Excel Export ]<br><br>    
								<table border=1 bordercolor=black cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100% bgcolor=#CECEFF class=ttt>  
									<tr>
										<td width=4%>ID</td>
										<td width=4%>ED</td>
										<td width=10%><a href="">NAME</a></td>
										<td width=10%><a href="">CATEGORY</a></td>
										<td width=7%><a href="">STATUS</a></td>
										<td width=7%>Start time</td>
										<td width=7%>Start Supporter</td>
										<td width=20%>Phản hồi</td>
										<td width=20%>Xử lí</td>
									</tr>
								</table>
								<table border=1 bordercolor=black cellspacing=0 cellpadding=0 style="border-collapse:collapse" width=100%>			
								';
							$i = 0;
							$er = current_user_get_field( 'access_level' );
					if ($er <90)
					{
							$CheckReportOfMonth = mysql_query("select * from `mantis_care_report_table` WHERE carer='$t_current_user_name' 
									AND from_unixtime(start_time, '%e') >='$sDay' AND from_unixtime(start_time, '%e')<='$eDay'
									AND from_unixtime(start_time, '%c') >='$sMonth' AND from_unixtime(start_time, '%c')<='$eMonth'
									AND from_unixtime(start_time, '%Y') >='$sYear' AND from_unixtime(start_time, '%Y')<='$eYear'
									ORDER By id ASC
							");
					}
					else 
					{
							$CheckReportOfMonth = mysql_query("select * from `mantis_care_report_table` WHERE carer='$username' 
									AND from_unixtime(start_time, '%e') >='$sDay' AND from_unixtime(start_time, '%e')<='$eDay'
									AND from_unixtime(start_time, '%c') >='$sMonth' AND from_unixtime(start_time, '%c')<='$eMonth'
									AND from_unixtime(start_time, '%Y') >='$sYear' AND from_unixtime(start_time, '%Y')<='$eYear'
									ORDER By id ASC
							");
					}
					
					while ($row = mysql_fetch_array($CheckReportOfMonth))
					{
									$i++;
									$id	= $row['id'];
									$name = $row['name'];
									$phone = $row['phone'];
									$email = $row['email'];
									$sex = $row['sex'];
									$address = $row['address'];
									$city = $row['city'];
									$serial = $row['serial'];
									$lo = $row['lo'];
									$object = $row['object'];
									$status = $row['status'];
									$state = $row['state'];
									$confirm = $row['confirm'];
									$process = $row['process'];
									$receiver = $row['receiver'];
									$carer = $row['carer'];
									$start_time = (date("d-m-Y H:i:s",$row['start_time']));
									$QueryTime = (date("H",$row['start_time']));
									$QueryDay = (date("d",$row['start_time']));
									$QueryMonth = (date("m",$row['start_time']));
									$QueryYear = (date("y",$row['start_time']));
								
							// Max a last update

												$qr = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by cid"; 
												$resultt = mysql_query($qr) or die(mysql_error());
												
												$qt = "SELECT MAX(last_updated),last_updated, cid, note, resolver FROM mantis_carenote_table WHERE cid=$id Group by last_updated ORDER BY last_updated DESC LIMIT 0,1"; 
												$resultqt = mysql_query($qt) or die(mysql_error());
												// Print out result
								echo '<tr>
										<td width=4%   class='.$state.'>'.$i.'</td>
										<td width=4%   class='.$state.'><a href="view_care_report.php?do=view&id='.$id.'">'.$id.'</a></td>
										<td width=10%  class='.$state.'>'.$name.'</td>
										<td width=10%  class='.$state.'>['.$status.']<br>'.$object.'</td>
										<td width=7%  class='.$state.'>'.$state.' (<a href="")>'.$receiver.'</a>)</td>
										<td width=7%   class='.$state.'>'.$start_time.'</td>
										<td width=7%   class='.$state.'>'.$carer.'</td>
										<td width=20%  class='.$state.'c>'.nl2br(substr($confirm,0,300)).'</td>
										<td width=20%  class='.$state.'c>'.nl2br(substr($process,0,300)).'</td>
										</tr>';	
					} 
				
						echo '</table>';
				echo '<div align=center width=50%>
				<table width=80% height=200 border=1>
				<tr>
					<td width=50% class=sign valign=top><br><br><b>Người báo cáo<br><br><br><br><br><br><br><br><br>'.$t_current_real_name.'</td>
					<td width=50% class=sign valign=top><br><br><b>Người xác nhận<br> GD Trung Tâm DVKH<br><br><br><br><br><br><br><br>Lương Vũ An Bình</td>
				</tr>
				</table>
				</div>';
				
		}
		
		
		?>
					</td>
				</tr>
			</table>
</div>

<?php
	html_page_bottom();
